package Boundry;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Boundry.GenericTools.GenericTabel;
import Boundry.GenericTools.IPanelOperation;
import Boundry.GenericTools.ViewDataPanel;
import Controllers.GlobalData;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import sun.awt.GlobalCursorManager;

public class ViewReuestsTableListPanel extends ViewDataPanel implements IPanelOperation,ActionListener 
{

	private static final long serialVersionUID = 1L;
	private GenericTabel closeRequest;
	private GenericTabel openRequest;
	private JButton btnSearchCloseReq;
	private JButton btnSearchOpenReq;
	
	
	public GenericTabel getCloseRequest() {
		return closeRequest;
	}


	public GenericTabel getOpenRequest() {
		return openRequest;
	}


	public JButton getBtnSearchCloseReq() {
		return btnSearchCloseReq;
	}


	public JButton getBtnSearchOpenReq() {
		return btnSearchOpenReq;
	}


	public ViewReuestsTableListPanel() 
	{
		int width = GlobalData.FrameWidth/2 - 30, height = 270;
		
		JPanel closeReqPanel = new JPanel();
		closeReqPanel.setBounds(20, 50, width, height);
		mainPanel.add(closeReqPanel);
		closeReqPanel.setLayout(null);
		
		JPanel openReqPanel = new JPanel();
		openReqPanel.setBounds(20 +  20 +width, 50, width, height);
		mainPanel.add(openReqPanel);
		openReqPanel.setLayout(null);
		
		JLabel lblStep = new JLabel("Step 1 : Chose Request to view:");
		lblStep.setBounds(31, 23, 203, 14);
		mainPanel.add(lblStep);
		
		btnSearchCloseReq = new JButton();
		btnSearchCloseReq.setIcon(new ImageIcon(WorkerPanel.class.getResource("/ICONS/2_action_search.png")));
		btnSearchCloseReq.setBounds(384, 331, 54, 34);
		mainPanel.add(btnSearchCloseReq);
		
		btnSearchOpenReq = new JButton();
		btnSearchOpenReq.setIcon(new ImageIcon(WorkerPanel.class.getResource("/ICONS/2_action_search.png")));
		btnSearchOpenReq.setBounds(822, 331, 54, 34);
		mainPanel.add(btnSearchOpenReq);
				
		btnSearchCloseReq.addActionListener(this);
		btnSearchOpenReq.addActionListener(this);
		
		closeRequest = new  GenericTabel(width-10, height-20, "Close Request List");
		closeRequest.setBounds(0, 0, width, height);
		closeReqPanel.add(closeRequest); 
		
		
		openRequest = new  GenericTabel(width-10, height-20, "Open Request List");
		openRequest.setBounds(0, 0, width, height);
		openReqPanel.add(openRequest); 
		

  	 openRequest.getCellSelectionModel().addListSelectionListener(new ListSelectionListener() 
	    {
		@Override 
		public void valueChanged(ListSelectionEvent e) 
	      {
			closeRequest.clearSelection();
	      }	
		});
		
  	closeRequest.getCellSelectionModel().addListSelectionListener(new ListSelectionListener() 
	    {
		@Override 
		public void valueChanged(ListSelectionEvent e) 
	      {
			openRequest.clearSelection();  
	      }
	    });
  	  
	}
	
    public boolean isChosen() 
    {
    	if (isopenRequestChosen() || iscloseRequestChosen() ) return true;
    	return false;
    }
    public boolean isopenRequestChosen() 
    {
    	if (openRequest.isChosen()) return true;
    	return false;
    }
    public boolean iscloseRequestChosen() 
    {
    	if (closeRequest.isChosen()) return true;
    	return false;
    }
    public int getChosen()
    {
    	if(closeRequest.isChosen()) 
    		return closeRequest.getChosen();
    	if(openRequest.isChosen()) 
    		return openRequest.getChosen();
    	return -1;
    }
	
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		//TODO To be Finish
		if (e.getSource() == btnSearchCloseReq)
		{
			String str = JOptionPane.showInputDialog(null, "Enter Id of Request : ", "Search Close Request", 1);
			if(isInteger(str))
			{
			   int id = Integer.parseInt(str);
			   
			}
			else
			{
				JOptionPane.showMessageDialog(null, "You need to enter only numbers.", "",2);
			}
		}
		else if (e.getSource() == btnSearchOpenReq)
		{
			String str = JOptionPane.showInputDialog(null, "Enter Id of Request : ", "Search Open Request", 1);
		}

	}
	
	public boolean isInteger(String s) {
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    // only got here if we didn't return false
	    return true;
	}
	
	@Override
	public String apHelpButton() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame frame = new JFrame();
					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setBounds(100, 100, 300, 300);
					ViewReuestsTableListPanel b = new ViewReuestsTableListPanel();
					frame.getContentPane().add(b);
					frame.setVisible(true);	  	    
			  
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	@Override
	public String setTitle() {
		// TODO Auto-generated method stub
		return null;
	}



}
